
/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: ByteField.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.dellroad.stuff.string.ParseContext;

/**
 * Byte field.
 *
 * @see org.dellroad.sidekar.annotation.SidekarField
 */
public final class ByteField extends AtomicField<Byte> {

    ByteField(String name, Entity<?> entity) {
        super(name, entity, Byte.class);
    }

    @Override
    public void visit(FieldSwitch fieldSwitch) {
        fieldSwitch.caseByteField(this);
    }

    @Override
    public void encode(DataOutputStream output, Byte value) throws IOException {
        output.writeByte(value);
    }

    @Override
    public Byte decode(DataInputStream input) throws IOException {
        return input.readByte();
    }

    @Override
    public String toString(Byte value) {
        return value.toString();
    }

    @Override
    public Byte fromString(ParseContext ctx) {
        return Byte.parseByte(ctx.matchPrefix("-?[0-9]+").group());
    }
}

